package com.kexun.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kexun.entity.DO.Permission;
import com.kexun.mapper.PermissionMapper;
import com.kexun.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<Permission> permissionList() {
        return permissionMapper.findAllPermission();
    }

    @Override
    public List<Permission> findByRoleId(int roleId) {
        //选中
        return permissionMapper.findByRoleId(roleId);
    }


    @Override
    public List<Permission> checkPermissionList(int roleId) {
        List<Permission> permissions = permissionList();//全部的
        List<Permission> hasPermissions = findByRoleId(roleId);//这个角色拥有的

        for (Permission permission : permissions) {
            for (Permission hasPermission : hasPermissions) {
                int id = permission.getId();
                int hasId = hasPermission.getId();
                if (id == hasId) {
                    permission.setCheck(true);
                }
            }
        }
        return permissions;
    }

    @Override
    public List<Integer> getHasIds(int roleId) {
        ArrayList<Integer> rets = new ArrayList<>();
//        List<Permission> permissions = permissionList();//全部的
        List<Permission> hasPermissions = findByRoleId(roleId);//这个角色拥有的
        for (Permission hasPermission : hasPermissions) {
            if (hasPermission.getCount() <= 0) {
                int hasId = hasPermission.getId();
                rets.add(hasId);
            }
        }
        return rets;
    }

    @Override
    public int removeByroleId(int rid, int pid) {
        return permissionMapper.removeByroleId(rid, pid);
    }

    @Override
    public int removeByroleId(int rid) {
        return permissionMapper.removeByroleId(rid);
    }

    @Override
    public int addPermissionId(int rid, int pid) {
        return permissionMapper.addPermissionId(rid, pid);
    }

    @Override
    public int deleteById(int id) {
        return permissionMapper.deleteById(id);
    }

    @Override
    public List<Map<String, Object>> findList() {
        return permissionMapper.findList();
    }

    @Override
    public Permission findPermissionById(int id) {
        return permissionMapper.selectById(id);
    }

    @Override
    public List<Permission> findPermissionList() {
        return permissionMapper.selectList(null);
    }

    @Override
    public int add(Permission permission) {
        return permissionMapper.insert(permission);
    }

    @Override
    public int edit(Permission permission) {
        return permissionMapper.updateById(permission);
    }

}
